package com.teradata.observer;

import com.teradata.observer.core.Event;
import com.teradata.observer.subject.Observer;
import com.teradata.observer.subject.Subject;
import com.teradata.observer.subject.SubjectEventType;

import java.lang.reflect.Method;

/**
 * Created by lyndon on 2018/5/29.
 */
public class ObserverTest {
    public static void main(String[] args) {

        try {

            // 观察者
            Observer observer = new Observer();
            // 从观察者类中，反射获取通知
            Method advice = Observer.class.getMethod("advice", new Class<?>[]{Event.class});

            // 这里写Lily
            Subject subject = new Subject();
            // 这里就是监听
            subject.addListener(SubjectEventType.ON_ADD, observer, advice);
            subject.addListener(SubjectEventType.ON_EDIT, observer, advice);
            subject.addListener(SubjectEventType.ON_REMOVE, observer, advice);
            subject.addListener(SubjectEventType.ON_QUERY, observer, advice);

            subject.add();
            subject.remove();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
